package com.hhw.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

//    后台监控功能(固定的代码)
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
//     后台需要有人登陆、账号密码
        HashMap<String, String> initParmeters = new HashMap<>();
//      增加配置
        initParmeters.put("loginUsername","admin");//登陆的key是固定的值
        initParmeters.put("loginPassword","123456");

        initParmeters.put("allow","");//允许谁可以访问

//        initParmeters.put("william","IP地址");//禁止谁不可以访问

        bean.setInitParameters(initParmeters);//设置初始化参数
        return bean;
    }
}
